var newsPage = new XFApp.client();
XFApp.client.prototype.newsPage = {
	 url : basePath+"/news/queryPage",
	 loadNews : function(divId,params,showPageCount,url){
		var _this = this;
		if(!!url){
			_this.url = url;
		}
     	$.ajax({
				url:_this.url,
				dataType:'json',
				type:'post',
				data:params,
				success:function(data){
					if(data.code==10000){
						$("#news-page-list").html($("#newsList_templete").render(data.result.itemList));
						_this.renderPageDiv(divId,data.result,params,showPageCount);
					}
				}
    		 });
     },
     
     renderPageDiv : function(divId,page,params,showPageCount){
    	var _this = this;
     	var pageDiv = '';
     	pageDiv+='<a href="javascript:void(0);" class="ui-paging-prev" localeString="previousPage">上一页</a>';
     	var startIndex = page.totalPage-page.pageNo>=showPageCount?page.pageNo:(page.totalPage-showPageCount>=1?page.totalPage-showPageCount:1);
     	var pageEnd = showPageCount+page.pageNo>page.totalPage?page.totalPage:showPageCount+page.pageNo;
     	for(var pageIndex = startIndex;pageIndex<=pageEnd;pageIndex++){
     		var curStr = '';
     		if(pageIndex == page.pageNo){
     			curStr = 'ui-paging-current';
     		}
     		if(pageIndex==pageEnd && pageEnd!=page.totalPage){
     			pageDiv+='<span class="ui-paging-ellipsis">...</span> <a href="javascript:void(0);" class="ui-paging-item">'+page.totalPage+'</a>';
     		}else{
     			pageDiv+='<a href="javascript:void(0);" class="ui-paging-item '+curStr+'">'+pageIndex+'</a>';
     		}
     	}
     	pageDiv+='<a href="javascript:void(0);" class="ui-paging-next" localeString="nextPage">下一页</a>';
     	pageDiv+='<span class="ui-paging-info"><span class="ui-paging-bold">'+page.pageNo+'/'+page.totalPage+'</span><span localeString="page">页</span></span>';
     	pageDiv+='<span class="ui-paging-which"><input type="text" name="some_name" value="'+page.pageNo+'"></span>';
     	pageDiv+='<a class="ui-paging-info ui-paging-goto" href="javascript:void(0);" localeString="gotoPage">跳转</a>';
     	$("#"+divId).html('<div class="ui-paging">'+pageDiv+'</div>');
     	//$("#"+divId+" *[localeString]").each(function(){
  		//  var localeName = $(this).attr("localeString");
  		//  $(this).html(localeLangues[localeName]);
     	//});
     	$("#"+divId+" .ui-paging-prev").on("click",function(){
     		if(page.pageNo>1){
     			params['pageNo']=page.pageNo-1;
     			_this.loadNews(divId,params,showPageCount);
     		}
     	});
     	$("#"+divId+" .ui-paging-next").on("click",function(){
     		if(page.pageNo<page.totalPage){
     			params['pageNo']=page.pageNo+1;
     			_this.loadNews(divId,params,showPageCount);
     		}
     	});
     	
     	$("#"+divId+" .ui-paging-item").on("click",function(){
     		if($(this).hasClass("ui-paging-current")){
     			return;
     		}
     		params['pageNo']=$(this).text();
 			_this.loadNews(divId,params,showPageCount);
     	});
     	$("#"+divId+" .ui-paging-goto").on("click",function(){
				var goPage = $("#"+divId+" input[name='some_name']").val();
				if(!isNaN(goPage) && goPage>=1 && goPage<= page.totalPage && goPage!=page.pageNo){
					params['pageNo']=goPage;
		 			_this.loadNews(divId,params,showPageCount);
				}else{
					$("#"+divId+" input[name='some_name']").val("");
				}
     	});
     }	
}
